package homework;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: fanbo
 * @Date: 2021/1/25 6:00 上午
 */
public class Request {
    private InputStream inputStream;

    /**
     * 请求方式
     */
    private String method;
    /**
     * 请求地址
     */
    private String url;

    /**
     * 端口号
     */
    private String port;

    /**
     * 主机地址
     */
    private String address;

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        // 输入流数据长度
        int count = 0;
        // 针对网络IO间断
        while (count ==0){
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        // 请求信息
        String inputStr = new String(bytes);

        // 第一行： GET / HTTP/1.1
        String firstLineStr = inputStr.split("\n")[0];
        String[] strings = firstLineStr.split(" ");
        this.method = strings[0];
        this.url = strings[1];
        System.out.println("method: "+method);
        System.out.println("url: "+url);

        // 第二行：Host: localhost:8080
        String secondLineStr = inputStr.split("\n")[1];
        String[] split = secondLineStr.split(":");
        this.address = split[1].trim();
        this.port = split[2].trim();
        System.out.println("address: "+address);
        System.out.println("port: "+port);

    }




    public Request() {
    }


    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
